package com.zzsy.carosshopprojects.util;

import android.bluetooth.BluetoothDevice;

import am.util.printer.PrintRequest;
import am.util.printer.PrintTask;
import am.util.printer.PrinterWriter80mm;

public class BlueToothPrintTask extends PrintTask {
    public BlueToothPrintTask(BluetoothDevice device, int type) {
        super(device, type);
    }

    public BlueToothPrintTask(String ip, int port, int type) {
        super(ip, port, type);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //打印前的准备工作，比如显示对话框
    }

    @Override
    protected byte[] getPrintData(int type) throws Exception {
        //实现打印数据的排版，生成字节流
        //根据不同的type创建不同的PrinterWriter，包内提供PrinterWriter80mm和PrinterWriter58mm，对应的是80mm小票打印机及58mm小票打印机。也可以自己创建PrinterWriter
        PrinterWriter80mm printer = new PrinterWriter80mm();
        printer.setAlignCenter();
       //printer.printDrawable(res, R.drawable.ic_printer_logo);
        printer.setAlignLeft();
        printer.printLine();
        printer.printLineFeed();
        printer.setLineHeight(80);
        printer.print("车有福测试");
        printer.printLineFeed();
        printer.print("车有福测试");
        printer.printLineFeed();
        printer.print("车有福测试");
        printer.printLineFeed();
        printer.print("车有福测试");
        printer.printLineFeed();
        printer.print("车有福测试");
        printer.printLineFeed();
        printer.print("车有福测试");
        printer.printLineFeed();
        printer.print("车有福测试");
        printer.printLineFeed();

        printer.feedPaperCutPartial();
        return printer.getData();
    }

    @Override
    protected void onPrinterStateChanged(int state) {
        super.onPrinterStateChanged(state);
        //这些状态的变更是在主线程内
        switch (state) {
            case PrintRequest.STATE_0:
                //生成打印页面数据...
                break;
            case PrintRequest.STATE_1:
                ///生成数据成功，开始创建Socket连接...
                break;
            case PrintRequest.STATE_2:
                //创建Socket成功，开始发送测试数据...
                break;
            case PrintRequest.STATE_3:
                //获取输出流成功，开始写入打印页面数据...
                break;
            case PrintRequest.STATE_4:
                //写入打印页面数据成功，正在完成打印...
                break;
        }
    }

    @Override
    protected void onResult(int errorCode) {
        super.onResult(errorCode);
        switch (errorCode) {
            case PrintRequest.ERROR_0:
                //打印成功完成！
                break;
            case PrintRequest.ERROR_1:
                //生成打印页面数据失败！
                break;
            case PrintRequest.ERROR_2:
                //创建Socket失败！
                break;
            case PrintRequest.ERROR_3:
                //获取输出流失败！
                break;
            case PrintRequest.ERROR_4:
                //写入打印页面数据失败！
                break;
            case PrintRequest.ERROR_5:
                //必要的参数不能为空！
                break;
        }
    }
}
